package com.group.controller;


import com.group.entity.GroupsInformation;
import com.group.entity.ProjectInside;
import com.group.service.impl.GroupsInformationServiceImpl;
import com.group.service.impl.ProjectInsideServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */

@Controller
//@RequestMapping("/groups-information")
@RequestMapping("/g")
public class GroupsInformationController {

    @Autowired
    private GroupsInformationServiceImpl gfs;

    @Autowired
    private ProjectInsideServiceImpl pis;


    private boolean flag=true;



    //向前端传送session里面存放这个用户的id
    //往twoxingxi.html里面传
    @RequestMapping("/getUserIdBySession")
    @ResponseBody
    public int getUserIdBySession(HttpSession session){
        return (Integer)session.getAttribute("currentUserId");
    }

    //向前端传送session里面存放的当前用户所在小组的id
    //往project_detailed.html里传
    @RequestMapping("/getGroupIdBySession")
    @ResponseBody
    public int getGroupIdBySession(HttpSession session){
        return  pis.selectGroupIdByProjectId(Integer.parseInt((String) session.getAttribute("tProjectId")));
    }


    //获取projectId存到session中并且跳转去project_detailed页面
    @RequestMapping("/pd/{projectId}")
    public String toProject_detailed(@PathVariable String projectId,HttpSession session){
        session.setAttribute("tProjectId",projectId);
        return "Project_detailed";
    }

    //根据前端传回的数据修改权限：project_detailed.html这个页面使用到这个方法
    @RequestMapping("/updatelevel")
    @ResponseBody
    public int updateLevel(String memberLevel,String groupId,String member){
        return gfs.updateLevel(memberLevel, Integer.parseInt(groupId), member);
    }


    //根据组的编号来查找这个组的信息,project_detailed.html这个页面用到这个方法
    @RequestMapping("/queryById")
    @ResponseBody
    public GroupsInformation queryGroupById(String groupId, ModelAndView modelAndView) {
        GroupsInformation groupsInformation = gfs.queryGroupById(Integer.parseInt(groupId));
        modelAndView.addObject("groupInfo",groupsInformation);
        modelAndView.setViewName("Project_detailed");
        return groupsInformation;
    }


    //组长创建一个初始组(就是没有任何小组成员的一个组),在xingxi页面点击下一步后用到
    //并获取这个小组的groupId返回给前端
    @RequestMapping("/create")
    @ResponseBody
    public int createGroup(String masterId,HttpSession session){
        gfs.creatGroup(Integer.parseInt(masterId));
        int groupId=gfs.queryGroupIdByMasterId(masterId);//这个方法可以获取新创建的小组的id
        session.setAttribute("currentGroupId",groupId);
        return  groupId;
    }


    //1.显示所有小组的成员信息(管理员用)
    @RequestMapping("/all")
    @ResponseBody
    public List<GroupsInformation> getAllGroups() {
        return  gfs.showAllGroups();
    }


    //3.根据组长的id来查找这个组的信息
    @RequestMapping("/queryByMasterId/{masterId}")
    @ResponseBody
    public GroupsInformation queryGroupByMasterId(@PathVariable String masterId) {
        return gfs.queryGroupByMasterId(Integer.parseInt(masterId));
    }

    //4.组长删除一个成员(组长无法删除自己)
    @RequestMapping("/delete/{member}/{memberId}")
    @ResponseBody
    public String deleteMember(@PathVariable String member,@PathVariable String memberId ) {
        //需要从前端获知成员为第几个成员(如"memberTwoId"这个字符串)以及他的id
        if(gfs.deleteMember(member,Integer.parseInt(memberId))>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return "操作完成";
    }

    //5.组长添加一个新成员
    @RequestMapping("/add/{groupId}/{memberId}/{memberLevel}")
    @ResponseBody
    public String addMember(@PathVariable String groupId,@PathVariable String memberId,@PathVariable String memberLevel){
        //获取组的id和新成员的id和level
        //新成员的id不能和老成员的id重复
        if(gfs.judgeId(Integer.parseInt(memberId))==null) { //先判断新id是否已经存在
            if (gfs.addMember(Integer.parseInt(groupId), Integer.parseInt(memberId), memberLevel) == 0) {
                System.out.println("+++添加失败");
            } else {
                System.out.println("添加成功");
            }
        }else{
            System.out.println("---添加失败");
        }
        return "操作完成";
    }

    //6.组长更新一个成员信息(组长可以更新自己)
    @RequestMapping("/update/{groupId}/{member}/{memberId}/{memberLevel}")
    @ResponseBody
    public String updateMember(@PathVariable String groupId,@PathVariable String member,@PathVariable String memberId,@PathVariable String memberLevel){
        //需要知道组的id,成员是第几个人,id值,level值
        if(gfs.updateMember(Integer.parseInt(groupId),member,Integer.parseInt(memberId),memberLevel)>0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
        return "操作完成";
    }

    //7.根据组的id来查找具体的项目
    @RequestMapping("/p/{groupId}")
    @ResponseBody
    public ProjectInside selectProjectByGroupId (@PathVariable String groupId){
        return pis.selectProjectByGroupId(Integer.parseInt(groupId));
    }

    //8.根据组员的id查找其对应的权限
    @RequestMapping("/le/{memberId}")
    @ResponseBody
    public String queryLevelById(@PathVariable String memberId){
        String flag=gfs.queryLevelById(Integer.parseInt(memberId));
        System.out.println(flag);
        return flag;
    }




}


